package com.jzg.sys.manager;

import com.jzg.sys.api.base.BaseDataService;
import com.jzg.sys.dao.entity.BaseData;
import com.jzg.sys.dao.entity.BaseDataDetail;
import com.jzg.sys.dao.mapper.BaseDataDetailMapper;
import com.jzg.sys.dao.mapper.BaseDataMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
    * 基础数据编码 服务实现类
    * </p>
 *
 * @author lzp
 * @since 2019-02-22
 */
@Service
public class BaseDataManager extends ServiceImpl<BaseDataMapper, BaseData> implements BaseDataService {
    @Resource
    private BaseDataDetailMapper baseDataDetailDao;

    @Cacheable(value = "baseDataCache",key="#dataCode+#sysCode")
    public List<BaseDataDetail> findCacheBaseData(String dataCode,String sysCode) {
        System.out.println("缓存加入:"+dataCode+sysCode);
        return baseDataDetailDao.findByDataCode(dataCode, null, sysCode);
    }
    @Override
    public List<BaseDataDetail> findCacheBaseData(String dataCode, String exceptDetailCodes, String sysCode) {
        List<BaseDataDetail> details = findCacheBaseData(dataCode, sysCode);
        return details.stream().filter( d->{
            String[] detailCodeArray = null;
            if (StringUtils.isNotBlank(exceptDetailCodes)) {
                detailCodeArray = exceptDetailCodes.split(",");
                for (String str : detailCodeArray) {
                    if(d.getDataDetailCode().equals(str)){
                        return false;
                    };
                }
            }
            return true;
        }).collect(Collectors.toList());
    }
}
